package com.topscore.uke.ui.activities;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.topscore.uke.R;
import com.topscore.uke.entities.ActivitiesDetailBean;
import com.topscore.uke.mvp.contractors.GetActivitiesNewsDetailContractor;
import com.topscore.uke.mvp.presenters.GetActivitiesNewsDetailPresenter;
import com.topscore.uke.ui.ToolBarCompatActivity;
import com.topscore.uke.ui.loadsir_callback.ErrorCallBack;
import com.topscore.uke.ui.loadsir_callback.LoadingCallBack;
import com.topscore.uke.utils.SnackBarUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

public class NewsDetailActivity extends ToolBarCompatActivity implements GetActivitiesNewsDetailContractor.View {

    @Bind(R.id.webView)
    WebView webView;
    @Bind(R.id.progress_bar)
    ProgressBar progressBar;

    private String mWebUrl;

    private String mTitleCode;

    private LoadService mLoadService;

    private GetActivitiesNewsDetailContractor.Presenter mPresenter;

    private ActivitiesDetailBean mDetailBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = new GetActivitiesNewsDetailPresenter(this);
        mTitleCode = getIntent().getStringExtra("title_code");
        setContentView(R.layout.activity_news_detail);
        ButterKnife.bind(this);
        initViews();
    }

    private void initViews() {
        mLoadService = LoadSir.getDefault().register(webView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                initData();
            }
        });
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setUseWideViewPort(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setLoadsImagesAutomatically(true);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    progressBar.setVisibility(View.GONE);
                } else {
                    progressBar.setVisibility(View.VISIBLE);
                    progressBar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }
        });
        initData();
    }

    private void initData() {
        mPresenter.getNewsData(mTitleCode);
    }

    @Override
    public void onLoading(String loadingMessage) {
        mLoadService.showCallback(LoadingCallBack.class);
    }

    @Override
    public void onLoadFinished() {
        mLoadService.showSuccess();
    }

    @Override
    public void onFail(int code, String errorMessage) {
        mLoadService.showCallback(ErrorCallBack.class);
        Toast.makeText(mContext, errorMessage, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onGetDataSuccess(ActivitiesDetailBean detailBean) {
        mDetailBean = detailBean;
        setCentralTitle(detailBean.getTitle());
        mWebUrl = detailBean.getUrl();
        webView.loadUrl(detailBean.getUrl());
    }
}
